/**
 * Created by xiewei on 10/19/15.
 */
var hospitals = new Array();//所有医院数据
var hosIndex = new Array();//医院列表索引
var imgDir = '../img/';
/**
 * 构造对象
 */
//医院
function Hospital(){}
Hospital.prototype = {
    id: '',
    name: '',
    level: '',
    time: [],
    coverImg: '',
    logoImg: '',
    address: [],
    tel: '',
    intro: '',
    character: '',
    docList: [],
    equipList: [],
    picList: []
}
//医生
function Doctor(){}
Doctor.prototype = {
    hosId: '',
    name: '',
    post: '',
    intro: '',
    pic: ''
}
//设备
function Equipment(){}
Equipment.prototype = {
    hosId: '',
    name: '',
    detail: ''
}
/**
 * 等级icon
 */
var levelImg = {
    'A': 'icon_a@2x.png',
    'B': 'icon_b@2x.png'
}
/**
 * 默认图片
 */
var dfltLogo = 'icon.png';
var dfltCover = 'cover.jpg';
/**
 * 创建节点
 */
var createList = function(hospital){
    var li = $('<li>').attr('id',hospital.id);
    var div = $('<div>').append($('<p>').text('营业时间'))
                        .append($('<time>').text(hospital.time.join('-')));
    //封面图片
    var img = $('<img>').addClass('coverImg');
    if(hospital.coverImg=='' || hospital.coverImg==null){
        img.attr('src',imgDir+dfltCover);
    }else{
        img.attr('src',imgDir+hospital.id+'/'+hospital.coverImg);
    }
    var coverDiv = $('<div>').addClass('imgCover');
    //icon和等级图片
    var img1 = $('<img>');
    if(hospital.logoImg=='' || hospital.logoImg==null){
        img1.attr('src',imgDir+dfltLogo);
    }else{
        img1.attr('src',imgDir+hospital.id+'/'+hospital.logoImg);
    }
    var img2 = $('<img>').attr('src',imgDir+levelImg[hospital.level]);
    var imgDiv = $('<div>').addClass('logoImg').append(img1,img2);

    var h1 = $('<h1>').text(hospital.name);
    var address = $('<address>').text(hospital.address.join(' '));

    li.append(div,img,coverDiv,imgDiv,h1,address);
    $('.app_hos_list').append(li);
}
/**
 * 获取医院数据
 */
$.ajax({
    type: 'GET',
    dataType: 'xml',
    url: "hospitals.xml",
    success: function(data){
        var root = $('root',data)[0];
        $('hospital',root).each(function(){
            var _this = $(this);
            var id = $(this).attr('id');
            var hospital = new Hospital();

            hospital.id = id;
            hospital.name = _this.children('name').text();
            hospital.level = _this.find('level').text();
            var _time = _this.find('time');
            hospital.time = new Array();
            hospital.time.push(_time.children('open').text());
            hospital.time.push(_time.children('close').text());
            hospital.coverImg = _this.find('cover').text();
            hospital.logoImg = _this.find('logo').text();
            var _address = _this.find('address');
            hospital.address = new Array();
            hospital.address.push(_address.children('city').text());
            hospital.address.push(_address.children('area').text());
            hospital.address.push(_address.children('road').text());
            hospital.tel = _this.find('tel').text();
            hospital.intro = _this.find('intro').text();
            hospital.character = _this.find('character').text();

            var _docList = _this.find('doclist');
            hospital.docList = new Array();
            $('doctor',_docList).each(function(){
                var doctor = new Doctor();
                doctor.hosId = id;
                doctor.name = $(this).children('name').text();
                doctor.post = $(this).children('post').text();
                doctor.intro = $(this).children('intro').text();
                doctor.pic = $(this).children('pic').text();
                hospital.docList.push(doctor);
            });

            var _equiplist = _this.children('equiplist');
            hospital.equipList = new Array();
            $('equip',_equiplist).each(function(){
                var equip = new Equipment();
                equip.hosId = id;
                equip.name = $(this).children('name').text();
                equip.detail = $(this).children('detail').text();
                hospital.equipList.push(equip);
            });

            var _picList = _this.children('piclist');
            hospital.picList = new Array();
            $('pic',_picList).each(function(){
                hospital.picList.push($(this).text());
            });

            hospitals.push(hospital);
            hosIndex.push(id);
            createList(hospital);
        });
        $('.app_hos_list li').each(function(){
            var id = $(this).attr("id");
            $(this).find('.imgCover').on("click",function(){
                var index = $.inArray(id,hosIndex);
                if(index > -1){
                    //存到本地
                    var hosStr = JSON.stringify(hospitals[index]);
                    localStorage.selectedHospital = hosStr;
                    window.open('hospital_detail.html','_self');
                }
            })
        })
    }
});



